package com.fastCms.business.frame.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fastCms.business.frame.constant.CommonConst;
import com.fastCms.business.frame.enums.CloudAdminTypeEnum;
import com.fastCms.business.frame.pojo.entity.CloudUser;
import com.fastCms.business.frame.daos.CloudUserMapper;
import com.fastCms.business.frame.service.CloudUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 平台系统用户表 服务实现类
 * </p>
 *
 * @author liusq
 * @since 2019-05-26
 */
@Service
public class CloudUserServiceImpl extends ServiceImpl<CloudUserMapper, CloudUser> implements CloudUserService {

    @Override
    public CloudUser findByUsername(String username, String platform) {
        QueryWrapper<CloudUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        queryWrapper.eq("platform", platform);
        queryWrapper.eq("state", CommonConst.STATE.VALID);
        return this.getOne(queryWrapper);
    }

    @Override
    public CloudUser findById(String id) {
        QueryWrapper<CloudUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        queryWrapper.eq("state", CommonConst.STATE.VALID);
        return this.getOne(queryWrapper);

    }

    @Override
    public CloudUser findByPhone(String phone, String platform) {
        QueryWrapper<CloudUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        queryWrapper.eq("platform", platform);
        queryWrapper.eq("state", CommonConst.STATE.VALID);
        return this.getOne(queryWrapper);
    }


    @Override
    public CloudUser findByPhoneAndOrganizeCode(String phone, String organizeCode) {
        QueryWrapper<CloudUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("organize_code", organizeCode);
        queryWrapper.eq("phone", phone);
        queryWrapper.eq("state", CommonConst.STATE.VALID);
        return this.getOne(queryWrapper);
    }

    @Override
    public CloudUser findByPlatformAdmin(String organizeCode, String platform) {
        QueryWrapper<CloudUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("organize_code", organizeCode);
        queryWrapper.eq("platform", platform);
        queryWrapper.eq("admin_type", CloudAdminTypeEnum.SYSTEM_ADMIN.getCode());
        queryWrapper.eq("state", CommonConst.STATE.VALID);
        return this.getOne(queryWrapper);
    }

    @Override
    public Boolean updateUserStatus(String organizeCode, Integer status) {
        UpdateWrapper<CloudUser> queryWrapper = new UpdateWrapper<>();
        queryWrapper.eq("organizeCode", organizeCode);
        queryWrapper.set("status", status);
        boolean update = this.update(queryWrapper);
        return update;
    }

    @Override
    public Boolean isCanChangeNewPhone(String userId, String phone) {
        UpdateWrapper<CloudUser> queryWrapper = new UpdateWrapper<>();
        queryWrapper.eq("phone", phone);
        queryWrapper.eq("state", CommonConst.STATE.VALID);
        queryWrapper.notIn("id", userId);
        int count = this.count(queryWrapper);
        return count <= 0;
    }
}
